package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.starseas_phone.Entity.AddStuList;
import com.example.starseas_phone.Entity.Students;
import com.lljjcoder.citypickerview.widget.CityPickerView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class AddstudentActivity extends AppCompatActivity {

    private String TAG = "AddstudentActivity测试";

    private EditText addstu_StuName;
    private EditText addstu_StuNational;
    private RadioButton addstu_stusex1;
    private RadioButton addstu_stusex2;
    private EditText addstu_StuAge;
    private Spinner addstu_StuPoliticalLandscape;
    private TextView addstu_StuNativePlace;
    private EditText addstu_Stucarid;
    private EditText addstu_StuPhone;
    private EditText addstu_StuEmail;
    private Spinner addstu_StuClassId;
    private TextView addstu_StubackTime;
    private Button addstu_stu_btn;
    private Spinner addstu_StuStateOfTheSchool;

    private List<String> classlist =new ArrayList<String>();
    private String selectClassid="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addstudent);
        initclassid();
        initid();
        addstu_StubackTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickerDialog(addstu_StubackTime);
            }
        });
        addstu_stu_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AddStu();
            }
        });

        addstu_StuNativePlace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CityPickerView cityPickerView = new CityPickerView(AddstudentActivity.this);
                cityPickerView.setOnCityItemClickListener(new CityPickerView.OnCityItemClickListener() {
                    @Override
                    public void onSelected(String... citySelected) {
                        //省份
                        String province = citySelected[0];
                        //城市
                        String city = citySelected[1];
                        //区县
                        String district = citySelected[2];
                        //邮编
                        String code = citySelected[3];
                        //控件自己写两个edittext或者Xedittext用来显示文本代替下面两个个控件ID
                        //address_new_address用来展示地址
                        //address_new_zipcode用来展示邮编
                        addstu_StuNativePlace.setText(province+"-"+ city+"-"+ district);
                    }
                });
                cityPickerView.show();

            }
        });


        addstu_StuClassId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectClassid = classlist.get(i);
                addstu_StuClassId.setSelection(i,true);
                Log.d("selectClassid",selectClassid);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void initid(){
        addstu_StuName = (EditText) findViewById(R.id.addstu_StuName);
        addstu_StuNational = (EditText) findViewById(R.id.addstu_StuNational);
        addstu_stusex1 = (RadioButton) findViewById(R.id.addstu_stusex1);
        addstu_stusex2 = (RadioButton) findViewById(R.id.addstu_stusex2);
        addstu_StuAge = (EditText) findViewById(R.id.addstu_StuAge);
        addstu_StuPoliticalLandscape = (Spinner) findViewById(R.id.addstu_StuPoliticalLandscape);
        addstu_StuNativePlace = (TextView) findViewById(R.id.addstu_StuNativePlace);
        addstu_Stucarid = (EditText) findViewById(R.id.addstu_Stucarid);
        addstu_StuPhone = (EditText) findViewById(R.id.addstu_StuPhone);
        addstu_StuEmail = (EditText) findViewById(R.id.addstu_StuEmail);
        addstu_StuClassId = (Spinner) findViewById(R.id.addstu_StuClassId);
        addstu_StubackTime = (TextView) findViewById(R.id.addstu_StubackTime);
        addstu_StuStateOfTheSchool = (Spinner) findViewById(R.id.addstu_StuStateOfTheSchool);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        addstu_StubackTime.setText(simpleDateFormat.format(date));

        addstu_stu_btn = (Button) findViewById(R.id.addstu_stu_btn);
    }

    private void AddStu(){
        String StuName = addstu_StuName.getText().toString();
        int StuAge = Integer.parseInt(addstu_StuAge.getText().toString());
        String StuSex;
        if (addstu_stusex1.isChecked()){
            StuSex = "男";
        }else {
            StuSex = "女";
        }
        String StuPhone = addstu_StuPhone.getText().toString();
        String StuEmail = addstu_StuEmail.getText().toString();
        String StuCarid = addstu_Stucarid.getText().toString();
        String StuClassId = selectClassid;
        String StubackTime = addstu_StubackTime.getText().toString();
        String PoliticalLandscape;
        if (addstu_StuPoliticalLandscape.getSelectedItemId()==0){
            PoliticalLandscape="群众";
        }else if(addstu_StuPoliticalLandscape.getSelectedItemId()==1){
            PoliticalLandscape="团员";;
        }else{
            PoliticalLandscape="党员";
        }
        String NativePlace = addstu_StuNativePlace.getText().toString();
        String National = addstu_StuNational.getText().toString();
        String StateOfTheSchool;
        if (addstu_StuStateOfTheSchool.getSelectedItemId()==0){
            StateOfTheSchool="在校";
        }else{
            StateOfTheSchool="毕业";
        }
        Students students = new Students(StuName,StuAge,StuSex,StuPhone,StuEmail,StuCarid,StuClassId,StubackTime,PoliticalLandscape,NativePlace,National,StateOfTheSchool);
        AddStuListActivity.lists.add(students);
        Log.d("添加学生信息:","添加学生成功");
        finish();

    }

    //弹出选择日期的对话框
    private void showDatePickerDialog(final TextView textView){
        Calendar c = Calendar.getInstance();
        new DatePickerDialog(AddstudentActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                textView.setText(i+"-"+(i1+1)+"-"+i2);
            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();
    }

    private void initclassid(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request1 = new Request.Builder()
                            .url(Sto.NetIP+"/api/Class/GetAllClasses")
                            .get()
                            .build();
                    Response response = client.newCall(request1).execute();
                    String results = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(results);
                    String message = jsonObject1.getString("message");
                    JSONArray jsonArray = new JSONArray(message);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String ClassId = jsonObject.getString("ClassId");
                        classlist.add(ClassId);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddstudentActivity.this,R.layout.classid_item, R.id.text,classlist);
                            addstu_StuClassId.setAdapter(adapter);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }


}